
// @ts-nocheck
import {HEADER, HTTP_REQUEST_URL} from "../config/app";

const loginUrl = '/pages/common/nick_avatar'

/**
 * 发送请求
 */
function baseRequest(url: string, method: string, data: any): any {
    let header = HEADER;
    header.token = uni.getStorageSync('token');
    // @ts-ignore
    return new Promise((reslove, reject) => {
        uni.request({
            url: HTTP_REQUEST_URL + url,
            method: method as any,
            header: header,
            data: data || {},
            success: (res) => {
                const resData: any = res.data
                const code: string = resData.code
                const data = resData.data
                const msg = resData.msg
				if(code=='1'){
					reslove(resData)
					return false;
				}
                // @ts-ignore
                if (code.startsWith('F')) {
                    console.log("后台bug， debug使用", data, msg)
                    uni.showToast({
                        title: '服务器异常！',
                        icon: "error",
                        duration: 2000
                    })
                    reject("")
                } else if (code.startsWith('M')) {
                    console.log("直接弹窗提示")
                    // @ts-ignore
                    uni.showToast({
                        title: msg,
                        icon: "error",
                        duration: 2000
                    })
                    reject("")
                } else if (code.startsWith('B')) {
                    console.log("前端-参数错误，用户前端debug使用", data, msg)
                } else if (['Q001', 'Q004'].indexOf(code) !== -1) {
                    uni.removeStorageSync('token')
                    uni.redirectTo({
                        url: '/pages/common/login_code'
                    })
                    reject("")
                } else if ('Q006' === code) {
                    console.log("账号冻结；提示账号冻结;前端跳转登陆页面")
                    uni.showToast({
                        title: '账号冻结',
                        icon: "error",
                        duration: 2000
                    })
                    uni.navigateTo({
                        url: loginUrl
                    })
                    reject("")
                } else if ('Q007' === code) {
                    console.log("账号已加入黑名单；提示账号已拉黑;前端跳转登陆页面")
                    uni.showToast({
                        title: '账号已加入黑名单',
                        icon: "error",
                        duration: 2000
                    })
                    uni.navigateTo({
                        url: loginUrl
                    })
                    reject("")
                } else if ('Q002' === code) {
                    console.log("无权限；前端错误，用户不应该能调用到改接口。", data, msg)
                } else {
                    reslove(resData);
                }

            },
            fail: (_msg: any) => {
                reject('请求失败');
            }
        })
    });
}

const request = {
    get: (api: string, data?: any) => baseRequest(api, 'get', data),
    post: (api: string, data?: any) => baseRequest(api, 'post', data),
    put: (api: string, data?: any) => baseRequest(api, 'put', data),
    delete: (api: string, data?: any) => baseRequest(api, 'delete', data),
};


export default request;
